package org.example.book.service;

import org.example.book.enums.BookStatus;
import org.example.book.mapper.BookMapper;
import org.example.book.model.BookInfo;
import org.example.book.model.PageRequest;
import org.example.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class BookService {

    @Autowired
    private BookMapper bookMapper;


    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookLIstByPage(PageRequest pageRequest) {
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos =bookMapper.queryBookByPage(pageRequest.getOffset(),pageRequest.getPageSize());
        for (BookInfo bookInfo : bookInfos){
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }

        return new PageResult<>(bookInfos,count,pageRequest);
    }

    public BookInfo queryBookId(Integer bookId) {
        BookInfo bookInfo=  bookMapper.queryBookById(bookId);
        bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        return bookInfo;
    }

    public Integer updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer deleteBookById(Integer bookId) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);
        return bookMapper.updateBookById(bookInfo);
    }


    public Integer batchDeleteBookByIds(List<Integer> ids) {
       return bookMapper.batchDeleteBookByIds(ids);
    }
}
